package com.pan.admin.module.api.controller;

import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.date.format.DateTimeFormat;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.module.api.entity.qo.ApiKeyHistoryBillQO;
import com.pan.admin.module.api.entity.vo.ApiKeyHistoryBillDetailVO;
import com.pan.admin.module.api.entity.vo.ApiKeyHistoryBillVO;
import com.pan.admin.module.api.service.ApiKeyHistoryBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * api渠道历史账单
 *
 * @author WJB
 * @date 2024-08-16
 */
@RestController
@RequestMapping("/api/apiKeyHistoryBill")
public class ApiKeyHistoryBillController extends BaseController {

    @Autowired
    private ApiKeyHistoryBillService apiKeyHistoryBillService;

    /**
     * 查询渠道历史账单列表
     */
    @PreAuthorize("@ss.hasPermi('api:apiKeyHistoryBill:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApiKeyHistoryBillQO apiKeyHistoryBillQO) {

        Map<String, Object> params = apiKeyHistoryBillQO.getParams();
        String startTime = String.valueOf(params.get("beginTime"));
        String endTime = String.valueOf(params.get("endTime"));

        boolean hasNow = false;
        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
            hasNow = true;
        }

        String displayDateTime;
        //  拼接时间
        if (hasNow) {
            displayDateTime = "当日数据截止至" + endTime;
        } else {
            displayDateTime = startTime + "至" + endTime;
        }

        apiKeyHistoryBillQO.setStartTime(startTime);
        apiKeyHistoryBillQO.setEndTime(endTime);

        List<ApiKeyHistoryBillVO> list = apiKeyHistoryBillService.list(apiKeyHistoryBillQO, displayDateTime);

        return getDataTable(list);
    }

    /**
     * 查询渠道历史账单列表
     */
    @PreAuthorize("@ss.hasPermi('api:apiKeyHistoryBill:list')")
    @GetMapping("/getBillDetail")
    public TableDataInfo getBillDetail(ApiKeyHistoryBillQO apiKeyHistoryBillQO) {

        Map<String, Object> params = apiKeyHistoryBillQO.getParams();
        String startTime = String.valueOf(params.get("beginTime"));
        String endTime = String.valueOf(params.get("endTime"));

        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
        }

        apiKeyHistoryBillQO.setStartTime(startTime);
        apiKeyHistoryBillQO.setEndTime(endTime);

        List<ApiKeyHistoryBillDetailVO> list = apiKeyHistoryBillService.getBillDetail(apiKeyHistoryBillQO);
        return getDataTable(list);
    }
}
